package com.example.taobaou.ui.adapter;

import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.taobaou.R;
import com.example.taobaou.model.domain.IBsaeInfo;
import com.example.taobaou.model.domain.OnSellContent;
import com.example.taobaou.utils.LogUtils;
import com.example.taobaou.utils.TostUtils;
import com.example.taobaou.utils.UrlUtils;

import java.util.ArrayList;
import java.util.List;

public class OnSellContentAdapter extends RecyclerView.Adapter<OnSellContentAdapter.InnerHolder> {

    private List<OnSellContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean>
            mData = new ArrayList<>();
    private OnSellPageItemClickListener mCuntentItemClickLisenter = null;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_on_sell_content, parent, false);
        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        OnSellContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean mapDataBean = mData.get(position);
        holder.setData(mapDataBean);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mCuntentItemClickLisenter != null) {
                    mCuntentItemClickLisenter.onSellItemClickLisenter(mapDataBean);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        LogUtils.d(this, "data size -------------------------------------->" + mData.size());
        return mData.size();
    }

    public void setData(OnSellContent result) {
        mData.clear();
        mData.addAll(result.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data());
        LogUtils.d(this, "data size -------------------------------------->" + mData.size());
        notifyDataSetChanged();

    }

    /**
     * 下拉加载更多数据
     *
     * @param moreResult
     */
    public void onLoadedMore(OnSellContent moreResult) {
        List<OnSellContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean>
                moreData = moreResult.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data();
        int oldSize = moreData.size();
        this.mData.addAll(moreData);
        notifyItemRangeChanged(oldSize - 1, moreData.size());
        TostUtils.showToast("更新了" + moreData.size() + "件宝贝~~");

    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        private final ImageView mCover;
        private final TextView mTitle;
        private final TextView mOrigin_prise;
        private final TextView mOff_prise;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            mCover = itemView.findViewById(R.id.on_sell_content_cover);
            mTitle = itemView.findViewById(R.id.on_sell_content_title);
            mOrigin_prise = itemView.findViewById(R.id.on_sell_content_origin_prise);
            mOff_prise = itemView.findViewById(R.id.on_sell_content_off_prise);
        }

        public void setData(OnSellContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean mapDataBean) {
            mTitle.setText(mapDataBean.getTitle());
            String pict_url = mapDataBean.getPict_url();
            String targetUrl = UrlUtils.getTicket(pict_url);
            Glide.with(itemView.getContext()).load(targetUrl).into(mCover);

            String orgin_prise = mapDataBean.getZk_final_price();
            mOrigin_prise.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            mOrigin_prise.setText("¥" + orgin_prise);

            int coupon_amount = mapDataBean.getCoupon_amount();
            float orginpriseF = Float.parseFloat(orgin_prise);
            float off_prise = orginpriseF - coupon_amount;
            mOff_prise.setText(String.format("%.2f", off_prise));


        }
    }

    public void setOnSellPageItemClickListener(OnSellPageItemClickListener listener){
        this.mCuntentItemClickLisenter = listener;
    }

    public interface OnSellPageItemClickListener{
        void onSellItemClickLisenter(IBsaeInfo dataBean);
    }
}
